<!DOCTYPE html>
<html lang="zh-cmn-Hans">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- Tell the browser to be responsive to screen width -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" type="image/png" sizes="16x16" href="/static/img/favicon.ico"/>
    <title>数据质量检核平台</title>

    <link rel="stylesheet" type="text/css" href="/static/check/css/admin/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="/static/check/css/admin/style.css"/>
    <link rel="stylesheet" type="text/css" href="/static/css/icons.css"/>

    <link rel="stylesheet" type="text/css" href="/static/css/sweetalert.css"/>

    <style>
        .table {
            color: black;
        }

        .table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
            vertical-align: middle;
            text-align: center;
        }

        .ibox-tools {
            float: unset;
        }

        .ibox-title h5 {
            margin: unset;
            padding-top: 8px;
            margin-right: 30px;
        }

        .table-hover > tbody > tr:hover {
            background-color: aliceblue;
        }
    </style>
</head>

<body>
<div class="tnav row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-8">
        <h2 class="fl" style="color: #007bff;font-size: 21px;font-weight:500">菜单</h2>
        <ol class="breadcrumb fl">
            <li><a href="../../data/dashboard/">主页</a></li>
            <li><strong>菜单</strong></li>
        </ol>
    </div>
    <div class="col-sm-4" style="margin: 15px 0;">
        <button onclick="Commit();" class="btn btn-primary" style="float: right;">
            <i class="im-checkmark"></i>保存
        </button>
    </div>
</div>

<div class="row col-lg-8" style="width: 100%;">
    <div class="wrapper wrapper-content">
        <div class="ibox">
            <div class="ibox-content">
                <table class="table table-hover overview_table">
                    <tbody>
                    <tr>
                        <td>项目编号</td>
                        <td><input id="nameEn" type="text" class="form-control" required></input></td>
                    </tr>
                    <tr>
                        <td>项目名称</td>
                        <td><input id="name" type="text" class="form-control" required></input></td>
                    </tr>
                    <tr>
                        <td>状态</td>
                        <td>
                            <select class="form-control" id="status" required>
                                <option>启用</option>
                                <option>停用</option>
                            </select>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<footer class="navbar-fixed-bottom" style="line-height: 10px;font-size:13px;">
    <div class="footer">
        © 2019-2020 cdxt
    </div>
</footer>

<script type="text/javascript" src="/static/js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/static/js/sweetalert.min.js"></script>

<script>
    var type = 'create';
    var id = '';

    function getQueryString () {
        if (window.location.href.indexOf('?') < 0) return false;
        const url = window.location.href.split('?')[1]; //?后面的url
        const strs = url.split('&'); //每一个参数[key=value]
        var params = {};
        for (var str of strs) {
            str = str.split('=');
            params[str[0]] = str[1];
        }
        return params;
    };

    function addMenu(nameEn, name, url, status) {
        $.ajax({
            type: "POST",
            url: "../../api/backend/menu/add",
            contentType: 'application/json',
            data: JSON.stringify({
                menu_name_en: nameEn,
                menu_name: name,
                menu_url_dashboard: url.dashboard,
                menu_url_detail: url.detail,
                menu_url_rule: url.rule,
                menu_validity: status
            }),
            success: function (data) {
                if (data.stat === 200) {
                    swal({
                        text: "新增成功...",
                        icon: "success",
                        buttons: false,
                        timer: 1000
                    }).then(function () {
                        parent.window.renderTable({refresh: true});
                        setTimeout(() => {
                            window.parent.hideModal();
                        }, 1000)
                    });
                } else {
                    swal({
                        title: '新增失败！',
                        text: data.msg,
                        icon: "error",
                    })
                }
            },
            error: function (e) {
                swal({
                    title: "发生错误",
                    text: e,
                    icon: "error",
                })
            }
        })
    };

    function updateMenu(nameEn, name, url, status) {
        $.ajax({
            type: "POST",
            url: "../../api/backend/menu/update",
            contentType: 'application/json',
            data: JSON.stringify({
                id: id,
                menu_name_en: nameEn,
                menu_name: name,
                menu_url_dashboard: url.dashboard,
                menu_url_detail: url.detail,
                menu_url_rule: url.rule,
                menu_validity: status
            }),
            success: function (data) {
                if (data.stat === 200) {
                    swal({
                        text: "更新成功...",
                        icon: "success",
                        buttons: false,
                        timer: 1000
                    }).then(function () {
                        parent.window.renderTable({refresh: true});
                        setTimeout(() => {
                            window.parent.hideModal();
                        }, 1000)
                    });
                } else {
                    swal({
                        title: '更新失败！',
                        text: data.msg,
                        icon: "error",
                    });
                }
            },
            error: function (e) {
                swal({
                    title: "发生错误",
                    text: e,
                    icon: "error",
                })
            }
        })
    }

    function Commit() {
        // 判断数据源信息是否存在空值，存在空值则出现提示
        var nameEn = document.getElementById("nameEn");
        var name = document.getElementById("name");
        var status = document.getElementById("status");
        var obj_id = ["nameEn", "name", "status"];
        var objs = [nameEn, name, status];
        var null_cnt = 0;
        for (let i in objs) {
            if (objs[i].value == null || objs[i].value.length == 0) {
                document.getElementById(obj_id[i]).style.borderBottomColor = "#ff0000";
                null_cnt += 1;
            } else {
                objs[i].value = objs[i].value.trim();
            }
        }
        if (null_cnt > 0) {
            swal({
                text: "存在空值！",
                icon: "error",
                buttons: false,
                timer: 1000
            });
            return;
        }

        var url = {
            dashboard: `/data/dashboard/subcompany?company=${nameEn.value}&company_zh=${name.value}`,
            detail: `/data/result_detail?company=${nameEn.value}`,
            rule: `/check/rule?company=${nameEn.value}&risk_market=`
        };
        if (type == 'create') {
            addMenu(nameEn.value, name.value, url, status.value == '启用' ? 1 : 0);
        } else {
            updateMenu(nameEn.value, name.value, url, status.value == '启用' ? 1 : 0);
        }
    };

    $(document).ready(function(){
        var params = getQueryString();

        var nameEn = document.getElementById("nameEn");
        var name = document.getElementById("name");
        var status = document.getElementById("status");
        nameEn.value = params.name ? decodeURIComponent(params.nameEn) : '';
        name.value = params.name ? decodeURIComponent(params.name) : '';
        status.value = params.status === undefined ? '启用' : (params.status == '1' ? '启用' : '停用');

        type = params.name ? 'update' : 'create';
        id = params.id || '';
    });
</script>

</body>

</html>